package com.base.api.controller.selfOrder;

import com.base.api.dto.selfOrder.CommodityConversionRecordDTO;
import com.base.api.dto.selfOrder.*;
import com.base.api.service.selfOrder.SelfOrderApiService;
import com.base.api.vo.selfOrder.*;
import com.base.log.LockApiAn;
import com.base.log.LogOutAnnotation;
import com.base.log.WebLog;
import com.base.result.ResponseResult;
import com.base.self.common.SelfRuntimeException;
import com.base.self.common.SelfRuntimeOkException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.math.BigDecimal;

@RestController
@Slf4j
@RequestMapping("/api/selfOrder")
@Api(tags = "商品订单")
public class SelfOrderApiController {

    private final SelfOrderApiService selfOrderApiService;

    public SelfOrderApiController(SelfOrderApiService selfOrderApiService) {
        this.selfOrderApiService = selfOrderApiService;
    }

    @ApiOperation(value="拼团付款(wap)")
    @PostMapping(value = "/partakerConfirmPayWap", produces = "application/json;charset=UTF-8")
    @Validated
    @LogOutAnnotation(url = "/partakerConfirmPayWap")
    @LockApiAn(noRollbackFor = {SelfRuntimeOkException.class, SelfRuntimeException.class})
    public ResponseResult partakerConfirmPayWap(HttpServletRequest request, final @Valid PartakerConfirmPayWapDTO partakerConfirmPayWapDTO) throws Exception {
        return selfOrderApiService.partakerConfirmPayWap(request, partakerConfirmPayWapDTO);
    }

    @ApiOperation(value="拼团付款(app)")
    @PostMapping(value = "/partakerConfirmPayApp", produces = "application/json;charset=UTF-8")
    @Validated
    @LogOutAnnotation(url = "/partakerConfirmPayApp")
    @LockApiAn(noRollbackFor = {SelfRuntimeOkException.class, SelfRuntimeException.class})
    public ResponseResult partakerConfirmPayApp(HttpServletRequest request, final @Valid PartakerConfirmPayAppDTO partakerConfirmPayAppDTO) throws Exception {
        return selfOrderApiService.partakerConfirmPayApp(request, partakerConfirmPayAppDTO);
    }

    @ApiOperation(value="拼团付款(jsapi)")
    @PostMapping(value = "/partakerConfirmPayJsapi", produces = "application/json;charset=UTF-8")
    @Validated
    @LogOutAnnotation(url = "/partakerConfirmPayJsapi")
    @LockApiAn(noRollbackFor = {SelfRuntimeOkException.class, SelfRuntimeException.class})
    public ResponseResult partakerConfirmPayJsapi(HttpServletRequest request, final @Valid PartakerConfirmPayJsapiDTO partakerConfirmPayJsapiDTO) throws Exception {
        return selfOrderApiService.partakerConfirmPayJsapi(request, partakerConfirmPayJsapiDTO);
    }

    @ApiOperation(value="兑换商品支付")
    @PostMapping(value = "/conversionConfirmPay", produces = "application/json;charset=UTF-8")
    @Validated
    @LogOutAnnotation(url = "/conversionConfirmPay")
    @LockApiAn(noRollbackFor = {SelfRuntimeOkException.class, SelfRuntimeException.class})
    public ResponseResult conversionConfirmPay(final @Valid ConversionConfirmPayDTO conversionConfirmPayDTO) throws Exception {
        return selfOrderApiService.conversionConfirmPay(conversionConfirmPayDTO);
    }

    @ApiOperation(value="订单确认支付(app)")
    @PostMapping(value = "/orderConfirmPayApp", produces = "application/json;charset=UTF-8")
    @Validated
    @LogOutAnnotation(url = "/orderConfirmPayApp")
    @LockApiAn(noRollbackFor = {SelfRuntimeOkException.class, SelfRuntimeException.class})
    public ResponseResult orderConfirmPayApp(HttpServletRequest request, final @Valid OrderConfirmPayAppDTO orderConfirmPayAppDTO) throws Exception {
        return selfOrderApiService.orderConfirmPayApp(request, orderConfirmPayAppDTO);
    }

    @ApiOperation(value="订单确认支付(jsapi)")
    @PostMapping(value = "/orderConfirmPayJsapi", produces = "application/json;charset=UTF-8")
    @Validated
    @LogOutAnnotation(url = "/orderConfirmPayJsapi")
    @LockApiAn(noRollbackFor = {SelfRuntimeOkException.class, SelfRuntimeException.class})
    public ResponseResult orderConfirmPayJsapi(HttpServletRequest request, final @Valid OrderConfirmPayJsapiDTO orderConfirmPayJsapiDTO) throws Exception {
        return selfOrderApiService.orderConfirmPayJsapi(request, orderConfirmPayJsapiDTO);
    }

    @ApiOperation(value="订单确认支付(wap)")
    @PostMapping(value = "/orderConfirmPayWap", produces = "application/json;charset=UTF-8")
    @Validated
    @LogOutAnnotation(url = "/orderConfirmPayWap")
    @LockApiAn(noRollbackFor = {SelfRuntimeOkException.class, SelfRuntimeException.class})
    public ResponseResult orderConfirmPayWap(HttpServletRequest request, final @Valid OrderConfirmPayWapDTO orderConfirmPayWapDTO) throws Exception {
        return selfOrderApiService.orderConfirmPayWap(request, orderConfirmPayWapDTO);
    }

    @ApiOperation(value="订单查询")
    @PostMapping(value = "/selfOrderSearch", produces = "application/json;charset=UTF-8")
    @Validated
    @LogOutAnnotation(url = "/selfOrderSearch")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = SelfOrderSearchVO.class),
    })
    public ResponseResult selfOrderSearch(final @Valid SelfOrderSearchDTO selfOrderSearchDTO) {
        return selfOrderApiService.selfOrderSearch(selfOrderSearchDTO);
    }

    @ApiOperation(value="订单删除")
    @PostMapping(value = "/selfOrderDel", produces = "application/json;charset=UTF-8")
    @Validated
    @LogOutAnnotation(url = "/selfOrderDel")
    public ResponseResult selfOrderDel(final @Valid SelfOrderDelDTO selfOrderDelDTO) {
        return selfOrderApiService.selfOrderDel(selfOrderDelDTO);
    }

    @ApiOperation(value="订单详情")
    @PostMapping(value = "/selfOrderDetails", produces = "application/json;charset=UTF-8")
    @Validated
    @LogOutAnnotation(url = "/selfOrderDetails")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = SelfOrderDetailsVO.class),
    })
    public ResponseResult selfOrderDetails(final @Valid SelfOrderDetailsDTO selfOrderDetailsDTO) {
        return selfOrderApiService.selfOrderDetails(selfOrderDetailsDTO);
    }

    @ApiOperation(value="拼中奖品列表")
    @PostMapping(value = "/selfOrderDeckList", produces = "application/json;charset=UTF-8")
    @Validated
    @LogOutAnnotation(url = "/selfOrderDeckList")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = SelfOrderDeckListVO.class),
    })
    public ResponseResult selfOrderDeckList(final @Valid SelfOrderDeckListDTO selfOrderDetailsDTO) {
        return selfOrderApiService.selfOrderDeckList(selfOrderDetailsDTO);
    }

    @ApiOperation(value="订单置换为奖励金")
    @PostMapping(value = "/selfOrderReplacement", produces = "application/json;charset=UTF-8")
    @Validated
    @LogOutAnnotation(url = "/selfOrderReplacement")
    @LockApiAn(noRollbackFor = {SelfRuntimeOkException.class, SelfRuntimeException.class})
    public ResponseResult selfOrderReplacement(final @Valid SelfOrderReplacementDTO selfOrderReplacementDTO) {
        return selfOrderApiService.selfOrderReplacement(selfOrderReplacementDTO);
    }

    @ApiOperation(value="支付宝通知")
    @RequestMapping(value = "/partakerAliNotify", method = {RequestMethod.GET, RequestMethod.POST})
    @LogOutAnnotation(url = "/partakerAliNotify", request = false)
    public void partakerAliNotify(HttpServletRequest request){
        selfOrderApiService.partakerAliNotify(request);
    }

    @ApiOperation(value="微信通知")
    @RequestMapping(value = "/partakerWxNotify", method = {RequestMethod.GET, RequestMethod.POST})
    @LogOutAnnotation(url = "/partakerWxNotify", request = false)
    public void partakerWxNotify(HttpServletRequest request) throws Exception {
        selfOrderApiService.partakerWxNotify(request);
    }

    @ApiOperation(value="支付宝退款")
    @RequestMapping(value = "/partakerAliRefund", method = {RequestMethod.POST})
    public ResponseResult partakerAliRefund(String orderNo, BigDecimal money){
        return selfOrderApiService.partakerAliRefund(orderNo, money);
    }

    @ApiOperation(value="微信退款")
    @RequestMapping(value = "/partakerWxRefund", method = {RequestMethod.POST})
    public ResponseResult partakerWxRefund(String orderNo, BigDecimal money){
        return selfOrderApiService.partakerWxRefund(orderNo, money);
    }

    @ApiOperation(value="测试失败通知重试")
    @RequestMapping(value = "/manualWxNotify", method = {RequestMethod.POST})
    @LogOutAnnotation(url = "/manualWxNotify", request = false)
    public ResponseResult manualWxNotify(){
        return selfOrderApiService.manualWxNotify();
    }

    @ApiOperation(value="失败通知重试")
    @RequestMapping(value = "/manualPartakerNotify", method = {RequestMethod.POST})
    @LogOutAnnotation(url = "/manualPartakerNotify", request = false)
    public ResponseResult manualPartakerNotify(String data){
        return selfOrderApiService.manualPartakerNotify(data);
    }

    @ApiOperation(value="最新拼中")
    @PostMapping(value = "/latestSpelling", produces = "application/json;charset=UTF-8")
    @LogOutAnnotation(url = "/latestSpelling")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = LatestSpellingVO.class),
    })
    public ResponseResult latestSpelling(@Validated LatestSpellingDTO latestSpellingDTO){
        return selfOrderApiService.latestSpelling(latestSpellingDTO);
    }

    @ApiOperation(value="即将拼中")
    @PostMapping(value = "/aboutSpelling", produces = "application/json;charset=UTF-8")
    @LogOutAnnotation(url = "/aboutSpelling")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = AboutSpellingVO.class),
    })
    public ResponseResult aboutSpelling(@Validated AboutSpellingDTO aboutSpellingDTO){
        return selfOrderApiService.aboutSpelling(aboutSpellingDTO);
    }

    @ApiOperation(value="最新拼中查看更多")
    @PostMapping(value = "/latestSpellingViewMore ", produces = "application/json;charset=UTF-8")
    @LogOutAnnotation(url = "/latestSpellingViewMore")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = LatestSpellingViewMoreVO.class),
    })
    public ResponseResult latestSpellingViewMore(@Validated LatestSpellingViewMoreDTO latestSpellingViewMoreDTO){
        return selfOrderApiService.latestSpellingViewMore(latestSpellingViewMoreDTO);
    }

    @ApiOperation(value="拼团记录")
    @PostMapping(value = "/partakerRecord ", produces = "application/json;charset=UTF-8")
    @LogOutAnnotation(url = "/partakerRecord")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = PartakerRecordVO.class),
    })
    public ResponseResult partakerRecord(@Validated PartakerRecordDTO partakerRecordDTO){
        return selfOrderApiService.partakerRecord(partakerRecordDTO);
    }

    @ApiOperation(value="用户兑换记录")
    @PostMapping(value = "/conversionRecord ", produces = "application/json;charset=UTF-8")
    @LogOutAnnotation(url = "/conversionRecord")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = ConversionRecordVO.class),
    })
    public ResponseResult conversionRecord(@Validated ConversionRecordDTO conversionRecordDTO){
        return selfOrderApiService.conversionRecord(conversionRecordDTO);
    }

    @ApiOperation(value="商品兑换记录")
    @PostMapping(value = "/commodityConversionRecord", produces = "application/json;charset=UTF-8")
    @Validated
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = CommodityConversionRecordVO.class),
    })
    @WebLog
    public ResponseResult commodityConversionRecord(final @Valid CommodityConversionRecordDTO commodityConversionRecordDTO){
        return selfOrderApiService.commodityConversionRecord(commodityConversionRecordDTO);
    }

    @ApiOperation(value="确认收货")
    @PostMapping(value = "/confirmReceipt", produces = "application/json;charset=UTF-8")
    @WebLog
    public ResponseResult confirmReceipt(final @Validated ConfirmReceiptDTO confirmReceiptDTO){
        return selfOrderApiService.confirmReceipt(confirmReceiptDTO);
    }


}
